package com.crayfish.quduo;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Process;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.crayfish.quduo.commom.Params;
import com.crayfish.quduo.utils.SharedPreferencesUtil;

import java.util.HashSet;
import java.util.Set;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.update.BmobUpdateAgent;


/**
 * Created by crayfish on 2016/11/24.
 */
public class App extends Application{

    private static App instance;
    private Set<Activity> allActivitys;

    public static int SCREEN_WIDTH = -1;
    public static int SCREEN_HEIGHT = -1;
    public static float DIMEN_RATE = -1.0F;
    public static int DIMEN_DPI = -1;

    public static synchronized App getInstance(){
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
//        //第一：设置BmobConfig，允许设置请求超时时间、文件分片上传时每片的大小、文件的过期时间(单位为秒)
//		BmobConfig config =new BmobConfig.Builder(this)
//		//设置appkey
//		.setApplicationId("9febbd0959baad8ed281a20874fcb8ba")
//		//请求超时时间（单位为秒）：默认15s
//		.setConnectTimeout(30)
//		//文件分片上传时每片的大小（单位字节），默认512*1024
//		.setUploadBlockSize(1024*1024)
//		//文件的过期时间(单位为秒)：默认1800s
//		.setFileExpiration(5500)
//		.build();
//		Bmob.initialize(config);
        //第二：默认初始化
        Bmob.initialize(this,"9febbd0959baad8ed281a20874fcb8ba","demo");
        //初始化屏幕宽高
        getScreenSize();
        initSetting();
    }

    public void addActivity(Activity activity){
        if(allActivitys == null){
            allActivitys = new HashSet<Activity>();
        }
        allActivitys.add(activity);
    }

    public void removeActivity(Activity activity){
        if(allActivitys != null){
            allActivitys.remove(activity);
        }
    }

    public void exitApp(){
        if(allActivitys != null){
            synchronized (allActivitys){
                for (Activity act : allActivitys){
                    act.finish();
                }
            }
        }
        Process.killProcess(Process.myPid());
        System.exit(0);
    }

    public void initSetting(){
        SharedPreferencesUtil.saveData(getApplicationContext(), Params.IS_NEW,false);
    }

    /**
     * 初始化屏幕宽高
     */
    public void getScreenSize(){
        WindowManager windowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        Display display =  windowManager.getDefaultDisplay();
        display.getMetrics(dm);
        DIMEN_RATE = dm.density/1.0F;
        DIMEN_DPI = dm.densityDpi;
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        if(SCREEN_WIDTH > SCREEN_HEIGHT){
            int t = SCREEN_HEIGHT;
            SCREEN_HEIGHT = SCREEN_WIDTH;
            SCREEN_WIDTH = t;
        }
    }
}
